//
//  FFProgressHUD.m
//  KeChuangManage
//
//  Created by NoNoKing on 2016/11/29.
//  Copyright © 2016年 shang. All rights reserved.
//

#import "FFProgressHUD.h"

#import <SVProgressHUD.h>

static CGFloat dismissTimeInterval = 0.5;

static FFProgressHUD *_instance;

@implementation FFProgressHUD

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (FFProgressHUD *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[FFProgressHUD alloc]initWithFrame:[UIScreen mainScreen].bounds];
//        _instance.backgroundColor = [UIColor clearColor];
    });
//    NSLog(@"_instance -- %p",_instance);
    UIWindow *widow = [UIApplication sharedApplication].keyWindow;
    [widow addSubview:_instance];
    return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}


- (void)showHUD:(NSString *)showMsg succeed:(BOOL)succeed
{
//    FFProgressHUD *proHUD = [self initProgressHUD];
    [SVProgressHUD setMinimumDismissTimeInterval:dismissTimeInterval];
    if (succeed) {
        [SVProgressHUD showInfoWithStatus:showMsg];
    }else{
        [SVProgressHUD showErrorWithStatus:showMsg];
    }
    
    
    [self hiddenHUD:self dismissTimeInterval:dismissTimeInterval];
}

- (instancetype)initProgressHUD
{
    FFProgressHUD *proHUD = [[FFProgressHUD alloc]initWithFrame:[UIScreen mainScreen].bounds];
    UIWindow *widow = [UIApplication sharedApplication].keyWindow;
    [widow addSubview:proHUD];
    return proHUD;
}

- (void)showLoadingWithTipMsg:(NSString *)tipMsg
{
    [SVProgressHUD showWithStatus:tipMsg];
}

- (void)showLoading
{
    [self showLoadingWithTipMsg:@"加载中..."];
}

#pragma mark - 设置UI

#pragma mark - 懒加载

#pragma mark - 设置数据

#pragma mark - 注册通知和接收通知

#pragma mark - 点击事件

#pragma mark - 业务逻辑
- (void)hiddenHUD:(UIView *)viewHUD dismissTimeInterval:(NSTimeInterval)interval
{
    double delayInSeconds = interval;
    __weak typeof(self) weakSelf = self;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [weakSelf hiddenHUD:viewHUD];
    });
}

- (void)hiddenHUD:(UIView *)viewHUD
{
    [viewHUD removeFromSuperview];
}
- (void)hiddenHUD
{
    [SVProgressHUD dismiss];
    [self removeFromSuperview];
}
@end
